package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.bean.History;
import com.example.bean.ScoreData;
import com.example.exception.ServiceException;
import com.example.service.HistoryService;
import com.example.service.ScoreService;
import com.example.until.Result;
import com.example.until.ResultCodeEnum;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @title: ScoreController
 * @Author wjf
 * @Date: 2022/10/16 10:39
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/score")
@CrossOrigin
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private HistoryService historyService;

    @Transactional
    @GetMapping("/add")
    public Result addScore(@RequestParam("borrowId") String borrowId,
                           @RequestParam("bookId") Integer bookId,
                           @RequestParam("score") Integer score) {
        try {
            ScoreData scoreData = new ScoreData();
            scoreData.setScore(score);
            scoreData.setBookId(bookId);
            scoreData.setBorrowId(borrowId);
            scoreService.save(scoreData);
            History history = new History();
            history.setStatusCom(1);  //评论成功后 评论状态设为1
            LambdaQueryWrapper<History> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(History::getBorrowId, borrowId);
            historyService.update(history,queryWrapper);
            return Result.ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(ResultCodeEnum.SERVICE_ERROR);
        }
    }
}

